{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9e9b7651",
   "metadata": {},
   "source": [
    "# Azure OpenAI\n",
    "\n",
    ":::caution\n",
    "You are currently on a page documenting the use of Azure OpenAI [text completion models](/docs/concepts/#llms). The latest and most popular Azure OpenAI models are [chat completion models](/docs/concepts/#chat-models).\n",
    "\n",
    "Unless you are specifically using `gpt-3.5-turbo-instruct`, you are probably looking for [this page instead](/docs/integrations/chat/azure_chat_openai/).\n",
    ":::\n",
    "\n",
    "This page goes over how to use LangChain with [Azure OpenAI](https://aka.ms/azure-openai).\n",
    "\n",
    "The Azure OpenAI API is compatible with OpenAI's API.  The `openai` Python package makes it easy to use both OpenAI and Azure OpenAI.  You can call Azure OpenAI the same way you call OpenAI with the exceptions noted below.\n",
    "\n",
    "## API configuration\n",
    "You can configure the `openai` package to use Azure OpenAI using environment variables.  The following is for `bash`:\n",
    "\n",
    "```bash\n",
    "# The API version you want to use: set this to `2023-12-01-preview` for the released version.\n",
    "export OPENAI_API_VERSION=2023-12-01-preview\n",
    "# The base URL for your Azure OpenAI resource.  You can find this in the Azure portal under your Azure OpenAI resource.\n",
    "export AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com\n",
    "# The API key for your Azure OpenAI resource.  You can find this in the Azure portal under your Azure OpenAI resource.\n",
    "export AZURE_OPENAI_API_KEY=<your Azure OpenAI API key>\n",
    "```\n",
    "\n",
    "Alternatively, you can configure the API right within your running Python environment:\n",
    "\n",
    "```python\n",
    "import os\n",
    "os.environ[\"OPENAI_API_VERSION\"] = \"2023-12-01-preview\"\n",
    "```\n",
    "\n",
    "## Azure Active Directory Authentication\n",
    "There are two ways you can authenticate to Azure OpenAI:\n",
    "- API Key\n",
    "- Azure Active Directory (AAD)\n",
    "\n",
    "Using the API key is the easiest way to get started. You can find your API key in the Azure portal under your Azure OpenAI resource.\n",
    "\n",
    "However, if you have complex security requirements - you may want to use Azure Active Directory. You can find more information on how to use AAD with Azure OpenAI [here](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/managed-identity).\n",
    "\n",
    "If you are developing locally, you will need to have the Azure CLI installed and be logged in. You can install the Azure CLI [here](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli). Then, run `az login` to log in.\n",
    "\n",
    "Add a role an Azure role assignment `Cognitive Services OpenAI User` scoped to your Azure OpenAI resource. This will allow you to get a token from AAD to use with Azure OpenAI. You can grant this role assignment to a user, group, service principal, or managed identity. For more information about Azure OpenAI RBAC roles see [here](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/role-based-access-control).\n",
    "\n",
    "To use AAD in Python with LangChain, install the `azure-identity` package. Then, set `OPENAI_API_TYPE` to `azure_ad`. Next, use the `DefaultAzureCredential` class to get a token from AAD by calling `get_token` as shown below. Finally, set the `OPENAI_API_KEY` environment variable to the token value.\n",
    "\n",
    "```python\n",
    "import os\n",
    "from azure.identity import DefaultAzureCredential\n",
    "\n",
    "# Get the Azure Credential\n",
    "credential = DefaultAzureCredential()\n",
    "\n",
    "# Set the API type to `azure_ad`\n",
    "os.environ[\"OPENAI_API_TYPE\"] = \"azure_ad\"\n",
    "# Set the API_KEY to the token from the Azure credential\n",
    "os.environ[\"OPENAI_API_KEY\"] = credential.get_token(\"https://cognitiveservices.azure.com/.default\").token\n",
    "```\n",
    "\n",
    "The `DefaultAzureCredential` class is an easy way to get started with AAD authentication. You can also customize the credential chain if necessary. In the example shown below, we first try Managed Identity, then fall back to the Azure CLI. This is useful if you are running your code in Azure, but want to develop locally.\n",
    "\n",
    "```python\n",
    "from azure.identity import ChainedTokenCredential, ManagedIdentityCredential, AzureCliCredential\n",
    "\n",
    "credential = ChainedTokenCredential(\n",
    "    ManagedIdentityCredential(),\n",
    "    AzureCliCredential()\n",
    ")\n",
    "```\n",
    "\n",
    "## Deployments\n",
    "With Azure OpenAI, you set up your own deployments of the common GPT-3 and Codex models.  When calling the API, you need to specify the deployment you want to use.\n",
    "\n",
    "_**Note**: These docs are for the Azure text completion models. Models like GPT-4 are chat models. They have a slightly different interface, and can be accessed via the `AzureChatOpenAI` class. For docs on Azure chat see [Azure Chat OpenAI documentation](/docs/integrations/chat/azure_chat_openai)._\n",
    "\n",
    "Let's say your deployment name is `gpt-35-turbo-instruct-prod`.  In the `openai` Python API, you can specify this deployment with the `engine` parameter.  For example:\n",
    "\n",
    "```python\n",
    "import openai\n",
    "\n",
    "client = AzureOpenAI(\n",
    "    api_version=\"2023-12-01-preview\",\n",
    ")\n",
    "\n",
    "response = client.completions.create(\n",
    "    model=\"gpt-35-turbo-instruct-prod\",\n",
    "    prompt=\"Test prompt\"\n",
    ")\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89fdb593-5a42-4098-87b7-1496fa511b1c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%pip install --upgrade --quiet  langchain-openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "faacfa54",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_VERSION\"] = \"2023-12-01-preview\"\n",
    "os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"...\"\n",
    "os.environ[\"AZURE_OPENAI_API_KEY\"] = \"...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8fad2a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import Azure OpenAI\n",
    "from langchain_openai import AzureOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8c80213a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create an instance of Azure OpenAI\n",
    "# Replace the deployment name with your own\n",
    "llm = AzureOpenAI(\n",
    "    deployment_name=\"gpt-35-turbo-instruct-0914\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "592dc404",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\" Why couldn't the bicycle stand up by itself?\\n\\nBecause it was two-tired!\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Run the LLM\n",
    "llm.invoke(\"Tell me a joke\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbfebea1",
   "metadata": {},
   "source": [
    "We can also print the LLM and see its custom print."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9c33fa19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1mAzureOpenAI\u001b[0m\n",
      "Params: {'deployment_name': 'gpt-35-turbo-instruct-0914', 'model_name': 'gpt-3.5-turbo-instruct', 'temperature': 0.7, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'logit_bias': {}, 'max_tokens': 256}\n"
     ]
    }
   ],
   "source": [
    "print(llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a8b5917",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "3bae61d45a4f4d73ecea8149862d4bfbae7d4d4a2f71b6e609a1be8f6c8d4298"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
